<%+cbi/valueheader%>
<link rel="stylesheet" href="/luci-static/vssr/css/vssr.css?v=72883">
<label class="cbi-value-title"><%= translate("Update All Subscribe Severs") %></label>
<div class="cbi-value-field">
	<input class="cbi-button cbi-button-reload" id="update_subscribe" type="button"
		size="0" value="<%= translate("Save And Start Subscribe") %>">
</div>

<script type="text/javascript">

	const SAVE_SUBSCRIBE_URL = '<%=luci.dispatcher.build_url("admin", "vpn", "vssr","subscribe")%>';
	const SERVERS_URL = '<%=luci.dispatcher.build_url("admin", "vpn", "vssr","servers")%>';

	var _responseLen;
	var noChange = 0;
	var modal = '<div class="modals-bg">' +
		'<div class="modals">' +
		'<h2><%:Subscription%></h2>' +
		'<h3 style="margin-left:0;"><%:Subscribing,Please do not refresh!%></h3>' +
		'<textarea cols="63" rows="28" wrap="on" readonly="readonly" id="log_content3" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"></textarea>' +
		'</div>' +
		'</div>';

	//显示并开始刷新订阅
	function update_subscribe() {
		$("body").append(modal);
		$(".modals-bg").show();
		setTimeout("get_realtime_log();", 500);
	}
	//保存订阅按钮
	$("#update_subscribe").click(function () {
		prefix_array = $("#cbi-vssr-server_subscribe .cbi-section-node").attr("id").split("-");
		prefix_array[0] = "cbid";
		prefix = prefix_array.join(".");
		if ($("[name='" + prefix + ".auto_update']").is(":checked")) {
			var auto_update = "1";
		} else {
			var auto_update = "0";
		}
		var auto_update_time = $("[name='" + prefix + ".auto_update_time']").val();
		var subscribe_url = [];
		$("[name='" + prefix + ".subscribe_url']").each(function () {
			if ($(this).val() != "") {
				subscribe_url.push($(this).val());
			}
		});
		if ($("[name='" + prefix + ".proxy']").is(":checked")) {
			var proxy = "1";
		} else {
			var proxy = "0";
		}

		var data = {
			auto_update: auto_update,
			auto_update_time: auto_update_time,
			subscribe_url: JSON.stringify(subscribe_url),
			proxy: proxy
		}
		//console.log(data);
		$.ajax({
			type: "post",
			url: SAVE_SUBSCRIBE_URL,
			dataType: "json",
			data: data,
			success: function (d) {
				if (d.error == 0) {
					update_subscribe();
				} else {
					alert("请至少填写一个订阅链接");
				}
			}
		});
		return false;
	});


	function get_realtime_log() {
		$.ajax({
			url: '/check_update.htm?v=' + parseInt(Math.random() * 100000000),
			dataType: 'html',
			error: function (xhr) {
				setTimeout("get_realtime_log();", 1000);
			},
			success: function (response) {
				var retArea = document.getElementById("log_content3");
				if (response.search("END SUBSCRIBE") != -1) {
					noChange++;
				}
				console.log(noChange);
				if (noChange > 10) {
					window.location.href = SERVERS_URL;
					return false;
				} else {
					setTimeout("get_realtime_log();", 250);
				}
				retArea.value = response;
				retArea.scrollTop = retArea.scrollHeight;
				_responseLen = response.length;
			},
			error: function () {
				setTimeout("get_realtime_log();", 500);
			}
		});
	}

</script>

<%+cbi/valuefooter%>